package com.jtmg.glorykingsnews.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jtmg.glorykingsnews.domain.TbManager;
import com.jtmg.glorykingsnews.domain.TbMarket;
import com.jtmg.glorykingsnews.mapper.TbManagerMapper;
import com.jtmg.glorykingsnews.vo.Market.MarketQuery;
import com.jtmg.glorykingsnews.vo.Market.MarketVoForQuery;
import com.jtmg.glorykingsnews.vo.commom.LayuiReturnVo;
import com.jtmg.glorykingsnews.vo.commom.MsgVo;
import com.jtmg.glorykingsnews.vo.goods.GoodsCategoryQuery;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.jtmg.glorykingsnews.mapper.TbMarketMapper;
import com.jtmg.glorykingsnews.service.TbMarketService;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TbMarketServiceImpl implements TbMarketService{

    @Resource
    private TbMarketMapper tbMarketMapper;

    @Resource
    private TbManagerMapper tbManagerMapper;

    Date date = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String currentTime = formatter.format(date);

    @Override
    public LayuiReturnVo<MarketVoForQuery> pageQuery(MarketQuery query) {
        Example example = new Example(TbMarket.class);
        Example.Criteria criteria = example.createCriteria().andEqualTo("deleted",0);
        String mName = query.getmName();
        String cName = query.getcName();
        if(mName != null){
            criteria.andLike("marketName","%"+mName+"%");
        }
        if (cName != null){
            criteria.andLike("city","%"+cName+"%");

        }
        int count = tbMarketMapper.selectCountByExample(example);
        List<TbMarket> marketList = tbMarketMapper.selectByExample(example);
        List<MarketVoForQuery> marketVoForQueries = new ArrayList<>();
        for(TbMarket market : marketList){
            MarketVoForQuery marketVoForQuery = new MarketVoForQuery();
            marketVoForQuery.setId(market.getId());
            marketVoForQuery.setMarketName(market.getMarketName());
            marketVoForQuery.setCity(market.getCity());
            marketVoForQuery.setAddress(market.getAddress());
            marketVoForQuery.setRunStartTime(market.getRunStartTime());
            marketVoForQuery.setRunEndTime(market.getRunEndTime());
            marketVoForQuery.setNotice(market.getNotice());
            marketVoForQuery.setDeleted(market.getDeleted());
            marketVoForQueries.add(marketVoForQuery);
        }
        // PageHelper.startPage(query.getPage(), query.getLimit());
        return new LayuiReturnVo<MarketVoForQuery>(0,"查询用户信息成功",count,marketVoForQueries);

    }

    @Override
    public MsgVo<TbMarket> queryTheMarket(String id) {
        try {
            //根据管理员id 查询商市id
            int marketId = tbManagerMapper.selectByPrimaryKey(id).getMid();
            //根据商市id查询商市的基本信息
            TbMarket market = tbMarketMapper.selectByPrimaryKey(marketId);
            return new MsgVo<>("200","查询商市信息成功",market);
        }catch (Exception e){
            return new MsgVo<>("201","查询商市信息异常");
        }
    }

    @Override
    public MsgVo createMarket(TbMarket market) {
        try{
            Example example = new Example(TbMarket.class);
            example.createCriteria().andEqualTo("marketName",market.getMarketName())
                    .andEqualTo("city",market.getCity())
                    .andEqualTo("deleted",0);
            List<TbMarket> marketList = tbMarketMapper.selectByExample(example);
            if(marketList.size()>0){
                return new MsgVo("201","该商市已存在，请重新添加");
            }
            market.setDeleted(0);
            tbMarketMapper.insert(market);
            return new MsgVo("200","新增成功");
        }catch (Exception e){
            return new MsgVo("201","新增异常");
        }
    }

    @Override
    public MsgVo deleteMarket(int id) {
        try {
            Example example = new Example(TbManager.class);
            example.createCriteria().andEqualTo("mid",id);
            TbManager manager = new TbManager();
            manager.setDeleted(1);
            //1 把相关管理员数据软删除   updateByExampleSelective() 这个方法  不更新实体里面的空字段
            tbManagerMapper.updateByExampleSelective(manager,example);
            //2 把该商市的信息删除
            //2.1 先根据id查出来这个商市的信息
            TbMarket market = tbMarketMapper.selectByPrimaryKey(id);
            //把该商市的delete字段设置为1，即已删除
            market.setDeleted(1);
            tbMarketMapper.updateByPrimaryKey(market);
            return new MsgVo("200","删除商市信息成功");
        }catch (Exception e){
            return new MsgVo("201","删除商市信息异常");
        }
    }

    @Override
    public MsgVo updateMarket(TbMarket market) {
        try {
            Example example = new Example(TbMarket.class);
            example.createCriteria().andEqualTo("id",market.getId());
            tbMarketMapper.updateByExampleSelective(market,example);
            return new MsgVo("200","修改商市信息成功");
        }catch (Exception e){
            return new MsgVo("201","修改商市信息异常");
        }
    }

    @Override
    public PageInfo<TbMarket> pageListByGoodsCategory(GoodsCategoryQuery query) {
        PageHelper.startPage(query.getPage(), query.getLimit());

        if (query.getCategoryName()!=null&&!"".equals(query.getCategoryName())){
            return new PageInfo<TbMarket> (tbMarketMapper.selectByCategory(query.getCategoryName()));
        }
        Example example = new Example(TbMarket.class);
        example.createCriteria().andEqualTo("deleted",0);
        return new PageInfo<TbMarket> (tbMarketMapper.selectByExample(example));
    }


}
